package com.zb.easypoi_demo;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zzb
 * @time: 2020/10/6 15:13
 */
public class Demo {
    public static void main(String[] args) throws Exception {
        Map<String,Object> map=new HashMap<>();
        //数据部分
        map.put("fhdh",1);
        map.put("xhdz","椒江杰克总部");
        List<Map<String,String>> list=new ArrayList<>();

        Map<String,String> xdMap=new HashMap<>();
        xdMap.put("fhdh","fhdh01");
        list.add(xdMap);
        Map<String,String> xdMap2=new HashMap<>();
        xdMap2.put("fhdh","fhdh01");
        list.add(xdMap2);

        map.put("xdList",list);
        XWPFDocument document = WordExportUtil.exportWord07("templates/fhmb.docx", map);
        FileOutputStream os=new FileOutputStream("c:test/a.docx");
        document.write(os);
        os.close();

        System.out.println("导出完成");

        word2pdf("C:\\test\\a.docx","C:\\test\\a.pdf");

    }

    public static boolean getLicense() throws Exception {
        boolean result = false;
        try {
            InputStream is = Document.class
                    .getResourceAsStream("/License.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    public static void word2pdf(String docPath,String pdfPath){
        try {
            if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
                throw new Exception("com.aspose.words lic ERROR!");
            }
            com.aspose.words.Document document = new com.aspose.words.Document(docPath);
            FileOutputStream os=new FileOutputStream(new File(pdfPath));
            document.save(os, SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
